<?php

namespace app\common\model\mysql;

use app\common\business\DataAuth;
use think\Model;

class SysMenu extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 新增前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeInsert($model){
        $userData = (new DataAuth())->getUserDetails();
        $model->create_by = $userData['userName'];
    }

    /**
     * 更新前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeUpdate($model)
    {
        $userData = (new DataAuth())->getUserDetails();
        $model->update_by = $userData['userName'];
    }

    /**
     * 查询菜单列表
     * @param $where
     * @param $field
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalMenuList($where, $field){
        $order = [
            "parent_id" =>"asc",
            "order_num" =>"asc",
        ];
        $result = $this->where($where)->field($field)->order($order)->select();

        return $result;
    }

    /**
     * 根据$parent_id,$menu_name 去数据库查询是否存在这条记录
     * @param $dict_type
     * @param $dict_label
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuByNameModel($parent_id,$menu_name,$menu_id){
        if(empty($parent_id) || empty($menu_name)){
            return false;
        }
        $where = [
            "parent_id" =>$parent_id,
            "menu_name"=>$menu_name
        ];
        if($menu_id){
            $result = $this->where("menu_id", "<>", $menu_id)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    /**
     * 根据id查询菜单数据
     * @param $id
     * @return SysDictType|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuByIdFind($id){
        if(empty($id)){
            return false;
        }
        $where = [
            "menu_id" =>$id,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 根据$parent_id查询下级是否有数据
     * @param $parent_id
     * @return SysMenu|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuByParentDataModel($parent_id){
        if(empty($parent_id)){
            return false;
        }
        $where = [
            "parent_id" =>$parent_id,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 根据$id删除菜单
     * @param $id
     * @return bool
     */
    public function DelMenuId($id){
        if(empty($id)){
            return false;
        }
        $where = [
            ['menu_id', '=', $id]
        ];
        $result = $this->where($where)->delete();
        return $result;
    }

    /**
     * 获取无限极分类列表
     * @param int $parentId 父级ID，默认为0表示顶级分类
     * @return array
     */
    public function getMenuTree($parentId = 0)
    {
        // 查询当前父级下的所有分类
        $map = ['parent_id' => $parentId];
        $categories = $this->where($map)->order('order_num', 'asc')->field("menu_id as id,menu_name as label")->select()->toArray();
        // 递归构建树形结构
        $tree = [];
        foreach ($categories as &$category) {
            // 递归获取子分类
            $category['children'] = $this->getMenuTree($category['id']);
            // 如果子分类为空，则移除'children'键（这一步是可选的，取决于你是否需要保留空数组）
            if (empty($category['children'])) {
                unset($category['children']);
            }
            // 将当前分类添加到树中
            $tree[] = &$category;
        }
        // 注意：由于使用了引用，需要取消引用以避免潜在的问题
        unset($category);
        return $tree;
    }
}
